package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhiqianbao.R;
import com.zhiqianbao.entity.Result;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.utils.ValidateUtils;

import java.util.HashMap;
import java.util.Map;
import cn.trinea.android.common.util.StringUtils;

/**
 * Created by liang on 2016/1/21.
 */
public class BindBankConfirmActivity extends BaseActivity implements View.OnClickListener{
    /**o
     * 开户银行
     */
    TextView textViewPayBankContent;
    /**
     * 银行卡号
     */
    TextView textViewPayBankNumberContent;
    /**
     * 绑定银行卡 手机号
     */
    EditText textViewPayBankPhoneContent;
    /**
     * 确认绑定按钮
     */
    Button buttonConfirm;
    /**
     * 取消按钮
     */
    Button buttonCancel;

    Map<String, String> dataMap;
    //UserInfo userInfo;
    //UserBank userBank;
    String bankcard;
    String dataJson;
    Gson gson = new Gson();

    Handler bandHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RESULT_CANCELED :
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case RESULT_OK :
                    Result<HashMap> data = (Result<HashMap>) msg.obj;
                    if(data!=null) {
                        if (data.getCode().equals(Constants.DEFAULT_SUCCESS_CODE)) {
                            //跳转到输入手机号页面
                            Map<String, Object> respones = data.getData();
                            String bindToken = (String) respones.get("bindToken");
                            String storableCardNo = (String) respones.get("storableCardNo");
                            String externalRefNumber = (String) respones.get("externalRefNumber");
                            dataMap.put("bindToken", bindToken);
                            dataMap.put("bankPhone", textViewPayBankPhoneContent.getText().toString());
                            dataMap.put("storableCardNo", storableCardNo);
                            dataMap.put("externalRefNumber", externalRefNumber);
                            String dataStr = gson.toJson(dataMap);
                            Intent phoneCodeConfirmIntent = new Intent(BindBankConfirmActivity.this, BindBankPhoneConfirmActivity.class);
                            phoneCodeConfirmIntent.putExtra("dataStr", dataStr);
                            startActivity(phoneCodeConfirmIntent);
                            BindBankConfirmActivity.this.finish();
                        } else {
                            showShortToast1(data.getMsg());
                        }
                    }
                    break;
            }
            closeWaitDialog();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_bank_confirm);
        init();
        initView();
    }

    private void initView() {
         textViewPayBankContent=(TextView)findViewById(R.id.textView_pay_bank_content);
         textViewPayBankNumberContent=(TextView)findViewById(R.id.textView_pay_bank_number_content);
         textViewPayBankPhoneContent=(EditText)findViewById(R.id.textView_pay_bank_phone_content);
         buttonConfirm=(Button)findViewById(R.id.button_confirm);
         buttonCancel=(Button)findViewById(R.id.button_cancel);

        dataJson = (String) getIntent().getSerializableExtra("dataJson");
        dataMap = gson.fromJson(dataJson, HashMap.class);

        bankcard = dataMap.get("bankCard");
        int l = bankcard.length() -  8;
        String x = "";
        for (int i = 0; i < l; i++) {
            x += "*";
        }
        bankcard = bankcard.substring(0, 4) + x + bankcard.substring(bankcard.length() - 4, bankcard.length());
        textViewPayBankNumberContent.setText(bankcard);
        textViewPayBankContent.setText(dataMap.get("bankName"));

        buttonConfirm.setOnClickListener(this);
        buttonCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(R.id.button_cancel==v.getId()) {
            finish();
        }
        else if(R.id.button_confirm==v.getId()){
                if (!ValidateUtils.isMobile(textViewPayBankPhoneContent.getText().toString())) {
                    showShortToast1("请填入手机号");
                    return;
                }
                showWaitDialog();
                //调用绑定银行卡接口
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = new Message();
                        Map<String, String> param = new HashMap<>();
                        param.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                        param.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                        param.put("bankCard", dataMap.get("bankCard"));
                        param.put("phoneNo", textViewPayBankPhoneContent.getText().toString());
                        param.put("cardHolderName", dataMap.get("name"));
                        param.put("cardHolderId", dataMap.get("idcard"));


                        String result = HttpUtils.httpPostString(Constants.API_ROOT + "/payment/bind", param);
                        if (StringUtils.isBlank(result)) {
                            message.what = RESULT_CANCELED;
                        } else {
                            message.what = RESULT_OK;
                            Result<HashMap> data = Result.fromJson(result, HashMap.class);
                            message.obj = data;
                        }
                        bandHandler.sendMessage(message);
                    }
                }).start();
        }
    }
}
